package com.colorful.insuranceproject.base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.lovely3x.common.managements.user.TokenManager;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.loginandresgiter.bean.SimpleUser;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/9/14.
 * <p>
 * 九州大药房的网页
 */
public class WebViewActivity extends TitleActivity {

    public static final String EXTRA_ID = "extra_id";

    @Bind(R.id.wb)
    WebView web;

    @Bind(R.id.tv_title)
    TextView tvTitle;

    private int type;

    @Override
    protected int getContentView() {
        return R.layout.activity_wv;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        getViewDivider().setVisibility(View.GONE);
        hiddenTitle();
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        type = bundle.getInt(EXTRA_ID);
    }

    @Override
    protected void onViewInitialized() {
        WebSettings settings = web.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                showProgressCircle("页面加载中....");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dismissProgressCircle();
            }
        });

        String mURL = null;
        if (type == 1) {  //  可乐否
            tvTitle.setText(getString(R.string.colorful));
            String token = TokenManager.getInstance().getTokeValue();
            mURL = "https://admin.colorfulflorist.com/test/wechat/index/index.html?appToken=" + token;
        } else if (type == 2) {  //  九州大药房
            tvTitle.setText(getString(R.string.jiuzhou_micro));
            SimpleUser simpleUser = (SimpleUser) UserManager.getInstance().getCurrentUser();
            String userId = simpleUser.getUid();
            String userName = simpleUser.getUserName();
            String userPhone = String.valueOf(simpleUser.getUserPhone());
            mURL = "http://m.dada360.com/klkj" + "?" + "colorfulUserId=" + userId + "&colorfulUserName=" + userName + "&colorfulUserPhone=" + userPhone;
        }
        web.loadUrl(mURL);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick({R.id.tv_close, R.id.tv_return})
    void onClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_close:
                finish();
                break;
            case R.id.tv_return:
                web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                web.goBack();
                break;
        }
    }

    /**
     * 该方法主要作用是充值TintBar的颜色  让沉浸式得到显示
     * 如果该类不需要改方法  可以重写 applyTranslationStatusBarAndNavigationBar   去掉super方法即可
     */
    @Override
    public Tint getTint() {
        Tint tint = super.getTint();
        tint.mStatusBarColor = getResources().getColor(R.color.web_title_color);
        return tint;
    }
}
